import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'layout',
    redirect: '/dashboard',
    component: () => import('@/layout/LayoutIndex.vue'),
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        meta: {
          name: '工作台',
          show: true
          // keepAlive: false
        },
        component: () => import('@/views/DashboardPage')
      },
      {
        path: '/manage-base-info/warehouse',
        name: 'warehouse',
        meta: {
          name: '仓库管理',
          show: true
          // keepAlive: false
        },
        component: () => import('@/views/ManageBaseInfo/WareHouse')
      },
      {
        path: '/manage-base-info/warehouse/details',
        // meta: {
        //   keepAlive: true
        // },
        component: () => import('@/views/ManageBaseInfo/details/WareHouse')
      },
      {
        path: '/manage-base-info/warehouse/details/:id',
        // meta: {
        //   keepAlive: false
        // },
        component: () => import('@/views/ManageBaseInfo/details/WareHouse')
      },
      {
        path: '/manage-base-info/area',
        name: 'area',
        meta: {
          name: '库区管理',
          show: true
          // keepAlive: false
        },
        component: () => import('@/views/ManageBaseInfo/AreaPage')
      },
      {
        path: '/manage-base-info/area/details',
        component: () => import('@/views/ManageBaseInfo/details/AreaDetail')
      },
      {
        path: '/manage-base-info/area/details/:id',
        component: () => import('@/views/ManageBaseInfo/details/AreaDetail')
      },
      {
        path: '/manage-base-info/location',
        name: 'location',
        meta: {
          name: '库位管理',
          show: true
          // keepAlive: false
        },
        component: () => import('@/views/ManageBaseInfo/LocationPage')
      },
      {
        path: '/manage-base-info/location/details',
        component: () => import('@/views/ManageBaseInfo/details/LocationDetail')
      },
      {
        path: '/manage-base-info/location/details/:id',
        component: () => import('@/views/ManageBaseInfo/details/LocationDetail')
      },
      {
        path: '/manage-base-info/locationView',
        name: 'locationView',
        meta: {
          name: '库位视图',
          show: true
          // keepAlive: false
        },
        component: () => import('@/views/ManageBaseInfo/LocationView')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/LoginPage')
  }
]

// 以 / 为开始建立简单的路由表索引
routes[0].children.forEach(item => {
  if (item?.children) {
    item.children.forEach(i => {
      if (i?.meta) store.commit('pushMenuIndex', { ...i.meta, path: i.path })
    })
  } else {
    if (item?.meta) store.commit('pushMenuIndex', { ...item.meta, path: item.path })
  }
})

const router = new VueRouter({
  routes
})

// 设置白名单
const whiteList = ['/login']

// 设置路由守卫
router.beforeEach((to, from, next) => {
  // 如果有 Token
  if (store.state.user.token) {
    // 如果去 /login 就转到 /
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    // 如果在白名单就放行
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
